package cn.tedu.blog.server.controller.api;

import cn.tedu.blog.server.pojo.dto.PageDTO;
import cn.tedu.blog.server.pojo.vo.ApiArticleListItemVO;
import cn.tedu.blog.server.pojo.vo.ArticleStandardVO;
import cn.tedu.blog.server.pojo.vo.*;
import cn.tedu.blog.server.service.IArticleService;
import cn.tedu.blog.server.utils.JsonPage;
import cn.tedu.blog.server.utils.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/articles")
@Api(tags = "前台文章模块")
public class ApiArticleController {
    @Autowired
    private IArticleService articleService;

    @PostMapping("/tag/{tagId}")
    @ApiOperation("根据分类id分页查询文章列表")
    public JsonResult countArticleByTagId(@PathVariable Long tagId) {
        log.debug("根据分类id分页查询文章列表"+tagId+"===========");
        Long count = articleService.countArticleBytagId(tagId);
        return JsonResult.ok(count);
    }

    @PostMapping("/tagList/tagId")
    @ApiOperation("根据分类id分页查询文章列表")
    public JsonResult listArticlesByTagId(@RequestBody PageDTO pageDTO) {
        log.debug("查询标签列表页码：{}",pageDTO);
        JsonPage<ApiArticleListItemVO> list = articleService.listArticlesByTagId(pageDTO.getTagId(), pageDTO);
        return JsonResult.ok(list);
    }



    @PostMapping("/list/categoryId")
    @ApiOperation("根据分类id分页查询文章列表")
    public JsonResult listArticlesByCategoryId( @RequestBody PageDTO pageDTO) {
        log.debug("查询分类列表页码：{}",pageDTO);
        JsonPage<ApiArticleListItemVO> list = articleService.listArticlesByCategoryId(pageDTO.getCategoryId(), pageDTO);
        return JsonResult.ok(list);
    }


    @PostMapping("/count/{categoryId}")
    @ApiOperation("根据分类id分页查询文章列表")
    public JsonResult countArticleByCategoryId(@PathVariable Long categoryId) {
       log.debug("根据分类id分页查询文章列表"+categoryId+"===========");
        Long count = articleService.countArticleByCategoryId(categoryId);
        return JsonResult.ok(count);
    }


    @ApiOperation("最新文章")
    @ApiOperationSupport(order = 100)
    @GetMapping("/selectNewestArticles")
    public JsonResult selectNewestArticles(){
        log.debug("正在执行查询最新文章");
        List<ArticleStandardVO> list=articleService.selectNewestArticle();
        return JsonResult.ok(list);
    }

    @ApiOperation("热门文章")
    @ApiOperationSupport(order = 200)
    @GetMapping("/selectPopularArticle")
    public JsonResult selectPopularArticle(){
        log.debug("正在查询热门文章");
        List<ArticleStandardVO> list = articleService.selectPopularArticle();
        return JsonResult.ok(list);
    }


    @ApiOperation("管理员推荐文章")
    @ApiOperationSupport(order = 300)
    @GetMapping("/selectIsRecommendArticle")
    public JsonResult selectIsRecommendArticle(){
        log.debug("正在查询管理员推荐文章");
        List<ArticleStandardVO> list = articleService.selectIsRecommendArticle();
        return JsonResult.ok(list);
    }



    @ApiOperation("查询文章列表")
    @ApiOperationSupport(order =400)
    @PostMapping("/selectArticleLists")
    public JsonResult selectArticleLists(@RequestBody PageDTO pageDTO){
        System.out.println("pageDTO = " + pageDTO.toString());
        log.debug("正在查询文章列表");
        JsonPage<ArticleStandardVO> list =
                articleService.selectArticleLists(pageDTO.getPageNum(), pageDTO.getSize());
        return JsonResult.ok(list);
    }

    @ApiOperation("查询文章的数量")
    @ApiOperationSupport(order =500)
    @GetMapping("/selectArticleNumbers")
    public JsonResult selectArticleCount(){
        log.debug("正在查询管理员推荐文章");
        Long count = articleService.selectArticleCount();
        return JsonResult.ok(count);
    }

    @ApiOperation("发布文章数")
    @GetMapping("/articleCount")
    public JsonResult articleCount(){
        log.debug("接收到查询发布文章数的请求");
        long count=articleService.articleCount();
        return JsonResult.ok(count);
    }


    @ApiOperation("查询用户个人文章(1.待审核，2.审核通过，3.审核失败)")
    @ApiOperationSupport(order = 600)
    @PostMapping("")
    public JsonResult savePersonalDetail(@RequestParam int pagSize, @RequestParam int auditStatus){
        log.info("查询用户博客,接收到参数:{},{}", pagSize, auditStatus);
        List<ArticlePersonalDetailVO> list = articleService.saveArticlePersonalDetail(pagSize, auditStatus);
        log.info("查询到用户文章数据:{}", list);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询用户发表的文章数")
    @ApiOperationSupport(order = 700)
    @PostMapping("/countByUserId")
    public JsonResult countByUserId(@RequestParam int auditStatus){
        log.info("查询用户发表的文章数");
        int articleSize = articleService.countArticleByUserId(auditStatus);
        return JsonResult.ok(articleSize);
    }

    @ApiOperation("查询用户收藏的文章")
    @ApiOperationSupport(order = 800)
    @GetMapping("/favoritr")
    public JsonResult favoritrArticle(int pagSize){
        log.info("查询用户收藏的文章列表,参数:{}", pagSize);
        List<FavoriteArticleVO> list = articleService.getCollectionArticle(pagSize);
        log.info("查询到用户收藏的文章数据:{}", list);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询文章浏览量")
    @ApiOperationSupport(order = 900)
    @GetMapping("/selectViewCount")
    public JsonResult selectViewCount(Long id){
        log.info("查询文章浏览量,参数:{}", id);

         int count = articleService.selectViewCount(id);
        log.info("查询文章浏览量:{}", count);
        return JsonResult.ok(count);
    }
}
